<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/30
 * @Description: 设备表
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class ImDevice extends BaseModel
{
    protected $table = 'im_device';

    /**
     * @desc 添加最近登录的设备 返回false 就是需要验证
     * @param array $user 用户对象
     * @param array $headerList 公共头
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addDevice($user, $headerList = [])
    {
        $userId = $user->id;
        $this->user_id = $userId;
        $this->device_id = $headerList['deviceuuid'][0] ?? '';
        $this->terminal = $headerList['terminal'][0] ?? '';
        $this->system_version = $headerList['systemversion'][0] ?? '';

        $device = $this->where(['user_id' => $userId, 'device_id' => $this->device_id])->first();
        if (!$device)
        {
            $this->save();
        }
        else
        {
            $device->updated_at = time();
            $device->save();

        }

        return true;
    }

    /**
     * @desc 删除设备号
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function delDevice($input, $userId = 0)
    {
        $device = $this->where(['user_id' => $userId, 'id' => $input['id']])->first();
        if (!$device)
        {
            return ['code' => 1, 'msg' => '删除失败'];
        }

        $device->status = 0;
        $device->save();

        return ['code' => 0, 'msg' => '删除成功'];
    }

    /**
     * @desc  登录的权限
     * @param array $user 用户对象
     * @param int $type 1手机密码登录， 2短信验证码登录
     * @param array $headerList 请求头数组
     * @return mixed
     * @author zhouzhou
     * @time 2021/12/15 17:44
     */
    public function isLoginPurview($user, $type = 1, $headerList = [])
    {
        $this->device_id = $headerList['deviceuuid'][0] ?? '';

        $userId = $user->id;
        $user->notice = $user->notice()->first();
        $smsVerify = $user->notice->sms_verify; // 新设备登录需要短信安全验证 默认0不需要，1打开需要
        $noPasswordLogin = $user->notice->no_password_login; // 禁止使用密码登录开关，默认0关闭可以使用，1打开不能密码登录
        $noSmsLogin= $user->notice->no_sms_login; // 禁止使用短信验证码登录开关，默认0关闭可以使用，1打开不能验证码登录

        $device = $this->where(['user_id' => $userId, 'device_id' => $this->device_id])->first();

        if ($type == 1 && $noPasswordLogin == 1)
        {
            return ['code'=>1, 'msg'=>'禁止使用密码登录','data'=>[]];
        }

        if ($type == 2 && $noSmsLogin == 1)
        {
            return ['code'=>1, 'msg'=>'禁止使用短信验证码登录','data'=>[]];
        }

        if (!$device && $smsVerify && $type == 1)
        {
            return ['code'=>0, 'msg'=>'新设备登录需要短信安全验证','data'=>[1]];
        }

        if ($device && $device['status'] == 0)
        {
            return ['code'=>0, 'msg'=>'删除设备登录需要短信安全验证','data'=>[1]];
        }

        // 完全正常
        return ['code'=>0, 'msg'=>'','data'=>[]];
    }

    /**
     * @desc 添加最近登录的设备 返回false 就是需要验证
     * @param array $user 用户对象
     * @param array $headerList 公共头
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/15 14:58
     */
    public function updateDevice($user, $headerList = [])
    {
        $userId = $user->id;
        $this->user_id = $userId;
        $this->device_id = $headerList['deviceuuid'][0] ?? '';
        $this->terminal = $headerList['terminal'][0] ?? '';
        $this->system_version = $headerList['systemversion'][0] ?? '';

        $device = $this->where(['user_id' => $userId, 'device_id' => $this->device_id])->first();
        if (!$device)
        {
            $this->save();
        }
        else
        {
            $device->status = 1;
            $device->updated_at = time();
            $device->save();

        }

        return true;
    }



}
